{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "import redis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15.4309999943\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "pool = redis.ConnectionPool(host='127.0.0.1', port=6379)\n",
    "r = redis.Redis(connection_pool=pool)\n",
    "kvalue = '111110000000111110000000'\n",
    "\n",
    "starttime = time.time()\n",
    "\n",
    "#kvmap = {'k1':kvalue,'k2':kvalue,'k3':kvalue,'k4':kvalue,'k5':kvalue,'k6':kvalue}\n",
    "for i in xrange(100000):\n",
    "    kvmap = {'k'+str(i+0):kvalue,'k'+str(i+1):kvalue,'k'+str(i+2):kvalue,'k'+str(i+3):kvalue,'k'+str(i+4):kvalue,'k'+str(i+5):kvalue}\n",
    "    r.hmset('hashkey', kvmap)\n",
    "    \n",
    "elapsetime = time.time() - starttime\n",
    "print elapsetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15.3150000572\n"
     ]
    }
   ],
   "source": [
    "st = time.time()\n",
    "for i in range(100000):\n",
    "    kvmap = {'k'+str(i+0):kvalue,'k'+str(i+1):kvalue,'k'+str(i+2):kvalue,'k'+str(i+3):kvalue,'k'+str(i+4):kvalue,'k'+str(i+5):kvalue}\n",
    "    r.mset(kvmap)\n",
    "    \n",
    "elapsetime = time.time() - st\n",
    "print elapsetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.67100000381\n"
     ]
    }
   ],
   "source": [
    "\n",
    "st = time.time()\n",
    "\n",
    "for i in xrange(10000):\n",
    "    pipe = r.pipeline()\n",
    "    kvmap = {'k'+str(60*i+0):kvalue,'k'+str(60*i+1):kvalue,'k'+str(60*i+2):kvalue,'k'+str(60*i+3):kvalue,'k'+str(60*i+4):kvalue,'k'+str(60*i+5):kvalue}\n",
    "    pipe.mset(kvmap)\n",
    "    kvmap = {'k'+str(60*i+6):kvalue,'k'+str(60*i+7):kvalue,'k'+str(60*i+8):kvalue,'k'+str(60*i+9):kvalue,'k'+str(60*i+10):kvalue,'k'+str(60*i+11):kvalue}\n",
    "    pipe.mset(kvmap)\n",
    "    kvmap = {'k'+str(60*i+12):kvalue,'k'+str(60*i+13):kvalue,'k'+str(60*i+14):kvalue,'k'+str(60*i+15):kvalue,'k'+str(60*i+16):kvalue,'k'+str(60*i+17):kvalue}\n",
    "    pipe.mset(kvmap)\n",
    "    kvmap = {'k'+str(60*i+18):kvalue,'k'+str(60*i+19):kvalue,'k'+str(60*i+20):kvalue,'k'+str(60*i+21):kvalue,'k'+str(60*i+22):kvalue,'k'+str(60*i+23):kvalue}\n",
    "    pipe.mset(kvmap)\n",
    "    kvmap = {'k'+str(60*i+24):kvalue,'k'+str(60*i+25):kvalue,'k'+str(60*i+26):kvalue,'k'+str(60*i+27):kvalue,'k'+str(60*i+28):kvalue,'k'+str(60*i+29):kvalue}\n",
    "    pipe.mset(kvmap)\n",
    "    kvmap = {'k'+str(60*i+30):kvalue,'k'+str(60*i+31):kvalue,'k'+str(60*i+32):kvalue,'k'+str(60*i+33):kvalue,'k'+str(60*i+34):kvalue,'k'+str(60*i+35):kvalue}\n",
    "    pipe.mset(kvmap)\n",
    "    kvmap = {'k'+str(60*i+36):kvalue,'k'+str(60*i+37):kvalue,'k'+str(60*i+38):kvalue,'k'+str(60*i+39):kvalue,'k'+str(60*i+40):kvalue,'k'+str(60*i+41):kvalue}\n",
    "    pipe.mset(kvmap)\n",
    "    kvmap = {'k'+str(60*i+42):kvalue,'k'+str(60*i+43):kvalue,'k'+str(60*i+44):kvalue,'k'+str(60*i+45):kvalue,'k'+str(60*i+46):kvalue,'k'+str(60*i+47):kvalue}\n",
    "    pipe.mset(kvmap)\n",
    "    kvmap = {'k'+str(60*i+48):kvalue,'k'+str(60*i+49):kvalue,'k'+str(60*i+50):kvalue,'k'+str(60*i+51):kvalue,'k'+str(60*i+52):kvalue,'k'+str(60*i+53):kvalue}\n",
    "    pipe.mset(kvmap)\n",
    "    kvmap = {'k'+str(60*i+54):kvalue,'k'+str(60*i+55):kvalue,'k'+str(60*i+56):kvalue,'k'+str(60*i+57):kvalue,'k'+str(60*i+58):kvalue,'k'+str(60*i+59):kvalue}\n",
    "    pipe.mset(kvmap)\n",
    "    #print i\n",
    "    pipe.execute()\n",
    "elapsetime = time.time() - st\n",
    "print elapsetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The slowest run took 4.25 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
      "10000 loops, best of 3: 143 µs per loop\n"
     ]
    }
   ],
   "source": [
    "%timeit r.mset(kvmap)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The slowest run took 5.33 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
      "10000 loops, best of 3: 153 µs per loop\n"
     ]
    }
   ],
   "source": [
    "%timeit r.hmset('hashkey', kvmap)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.00099992752\n"
     ]
    }
   ],
   "source": [
    "t1 = time.time()\n",
    "time.sleep(2)\n",
    "t2 = time.time() - t1\n",
    "print t2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用python写的脚本测试：\n",
    "单条指令：mset 3.14微秒最好，最差13微秒。hmset3.15微秒最好，最坏16微秒\n",
    "单次mset，hmset 进行10w条循环，相差不多，15秒左右\n",
    "pipeline模式，10条指令提交一次，1w循环 7-8秒"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
